<?php
class ZMMSYima
{

	static public function CheckOrder($order) {
		$coupon_array = array('yimamms');
		$team = Table::FetchForce('team', $order['team_id']);
		if (!in_array($team['delivery'], $coupon_array)) return;
		if ( $team['now_number'] >= $team['min_number'] ) {
			//init coupon create;
			$last = ($team['conduser']=='Y') ? 1 : $order['quantity'];
			$offset = max(5, $last);
			if ( $team['now_number'] - $team['min_number'] < $last) {
				$orders = DB::LimitQuery('order', array(
							'condition' => array(
								'team_id' => $order['team_id'],
								'state' => 'pay',
								),
							));
				foreach($orders AS $order) {
					self::Create($order);
				}
			}
			else{
				self::Create($order);
			}
		}
	}

	static public function Create($order) {
		$team = Table::Fetch('team', $order['team_id']);
		$partner = Table::Fetch('partner', $order['partner_id']);
		$ccon = array('order_id' => $order['id']);
		$count = Table::Count('yimamms', $ccon);
		while($count<$order['quantity']) {
			$id = '1000000286' . date('YmdHis', time()) . rand(100000, 999999);
			$id = Utility::VerifyCode($id);
			$cv = Table::Fetch('yimamms', $id);
			if ($cv) continue;
			
			/* 发送翼码二维码彩信 */
			$info = array(
				'req_seq' => $id,
				'user_id' => $order['user_id'],
				'batch_no' => $team['batch_no'], //活动号
				'phone_req' => $order['mobile'],
				'phone_rece' => $order['mobile'],
				'info_title' => iconv('UTF-8', 'gb2312//IGNORE', '第一团购网'),
				'notes' => iconv('UTF-8', 'gb2312//IGNORE', $team['product'].',有效期至'.date('YmdHis',$team['expire_time'] + 86400).',商家电话:'.$partner['phone']),
				'mms_notes' => iconv('UTF-8', 'gb2312//IGNORE', $team['product'].',有效期至'.date('YmdHis',$team['expire_time'] + 86400).',商家电话:'.$partner['phone'].',地址:'.$partner['address']),
				'begin_time' => date('YmdHis',$team['begin_time']),
				'valid_time' => date('YmdHis',$team['expire_time'] + 86400),
				'total_amt' => $team['team_price'],
				'total_times' => 1, //使用次数
				'serv_code' => $team['serv_code'], //商品代码
				'print_text' => iconv('UTF-8', 'gb2312//IGNORE', '谢谢使用！'), //打印文本
				'org_amt' => $team['team_price'],
				'org_times' => 1, //最大使用次数
			);
			$res = YimaMms::send_mms($info);
			$doc = new DOMDocument();
			$doc->loadXML($res);
			$sys_seq = $doc->getElementsByTagName("sys_seq")->item(0)->nodeValue;
			$rs = $doc->getElementsByTagName("id")->item(0)->nodeValue;
			$msg = $doc->getElementsByTagName("comment")->item(0)->nodeValue;
			/* 发送翼码二维码彩信 end */
			if('0000' == $rs) {
			    $mms = 1;
			}else {
			    $mms = 0;
			}
            $yimamms = array(
					'id' => $id,
					'sys_seq' => $sys_seq,
					'user_id' => $order['user_id'],
					'partner_id' => $team['partner_id'],
					'city_id' => $team['city_id'],
					'order_id' => $order['id'],
					'team_id' => $order['team_id'],
					'expire_time' => $team['expire_time'],
					'create_time' => time(),
					'msg' => $msg,
					'mms' => $mms,
				);
			if(DB::Insert('yimamms', $yimamms))
				//sms_coupon($coupon);
			$count = Table::Count('yimamms', $ccon);
		}
	}
}
